/*
 * Java CMPP API
 * Copyright (C) 1998 - 2002 by Xu Youming
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * 
 * A copy of the LGPL can be viewed at http://www.gnu.org/copyleft/lesser.html
 * Java CMPP API author: xuym@bc-info.net
 * Java CMPP API Homepage: http://CMPPapi.sourceforge.net/
 * $Id: EventDispatcher.java,v 1.1 2008/03/19 02:56:56 xym Exp $
 */

package com.bci.cmpp.event;

import com.bci.cmpp.Connection;
import com.bci.cmpp.message.CMPPPacket;

import java.util.Iterator;

/**
 * This interface defines the observable side of the observer pattern for
 * asynchronous CMPP event notification. Each {@link com.bci.cmpp.Connection}
 * object will have an implementation of the <code>EventDispatcher</code>
 * interface which it uses to deliver events to interested listeners. By
 * removing the actual dispatching of events from the internals of the
 * Connection, applications may provide their own event dispatch implementations
 * for their <code>Connection</code> objects which better suit how those
 * applications work.
 * 
 * @author Xu Youming
 * @see com.bci.cmpp.event.SimpleEventDispatcher
 */
public interface EventDispatcher {

    /**
     * Initialise the event dispatcher. The <code>init</code> method will be
     * called by the <code>Connection</code> before it makes any attempt to
     * add any observers or deliver any events via the dispatcher.
     */
    public void init();

    /**
     * Event dispatcher clean up. The <code>destroy</code> method will be
     * called by the <code>Connection</code> when it is finished delivering
     * events to it and the receiver daemon thread is exiting. Any initialising
     * done in the <code>init</code> method can be cleaned up here.
     * <p>
     * The <code>destroy</code> method <b>must not </b> interfere with the
     * delivery of any events notified to the event dispatcher before the call
     * to this method.
     * </p>
     */
    public void destroy();

    /**
     * Add an observer to this event dispatcher.
     * 
     * @param observer
     *            the observer object to add.
     */
    public void addObserver(ConnectionObserver observer);

    /**
     * Remove an observer from this event dispatcher.
     * 
     * @param observer
     *            the observer object to remove from the registered observers.
     */
    public void removeObserver(ConnectionObserver observer);

    /**
     * Get an iterator over the currently registered observers.
     * 
     * @return an iterator object which iterates over all registered observers.
     */
    public Iterator observerIterator();

    /**
     * Notify all registered observers of an CMPP event.
     * 
     * @param event
     *            the CMPP event to notify observers of.
     */
    public void notifyObservers(Connection conn, CMPPEvent event);

    /**
     * Notify all registered observers of a received CMPP packet.
     * 
     * @param packet
     *            the CMPP packet to notify observers of.
     */
    public void notifyObservers(Connection conn, CMPPPacket packet);
}